// pages/account/discount/coupon/index.js
var app = getApp();
Page({

	/**
	 * 页面的初始数据
	 */
  data: {
    images: {},
    imgurl: app.urlimg,
    coupon_lists: [],
    // 广告模板
    picture: [],
  },
  //点击获取优惠劵
  gain_click: function (e) {
    var that = this;
    var couponId = e.currentTarget.dataset.id;
    var index = e.currentTarget.dataset.index;
    var coupon_lists = that.data.coupon_lists;
    wx.request({
      url: app.url + "api_coupon/receive",
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync("token_key")
      },
      data: {
        couponId: couponId
      },
      success: function (result) {
        console.log(result);
        if (result.data.result != 'Flase') {
          coupon_lists[index].restricts--;
          if (coupon_lists[index].restricts != 0) {
            //提示信息
            wx.showToast({
              title: '还能领取' + coupon_lists[index].restricts + '次',
              icon: 'success',
              duration: 1000,
            })
          }
          that.setData({
            coupon_lists: coupon_lists,
          })
        } else {
          //提示信息
          wx.showToast({
            title: '您已领取过了',
            icon: 'loading',
            duration: 1200,
          })
        }
      }
    })
  },
  //新增广告位的页面跳转
  coupon_banner_click: function (e) {
    console.log('新增的广告位banner被点击了！');
    console.log(e.currentTarget.dataset.link);
    var url = e.currentTarget.dataset.link;
    wx.redirectTo({
      url: url,
    })
  },
  // 广告位图片大小控制
  imageLoad: function (e) {
    console.log(e);
    var $width = e.detail.width,    //获取图片真实宽度
      $height = e.detail.height,
      ratio = $width / $height;    //图片的真实宽高比例
    var viewWidth = 718,           //设置图片显示宽度，左右留有16rpx边距
      viewHeight = 718 / ratio;    //计算的高度值
    var image = this.data.images;
    //将图片的datadata-index作为image对象的key,然后存储图片的宽高值
    image[e.currentTarget.dataset.id] = {
      width: viewWidth,
      height: viewHeight
    }
    console.log(image);
    this.setData({
      images: image
    })
  },
  // 优惠劵专区的广告模板点击事件
  advertising_click: function (e) {
    console.log(e.currentTarget.dataset.url);
    var url = e.currentTarget.dataset.url;
    wx.redirectTo({
      url: url,
    })
  },
	/**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function (options) {
    var that = this;
    var token_key = wx.getStorageSync('token_key');
    console.log(token_key);
    if (token_key == undefined || token_key == '') {
      app.getUserInfo(function () {
        load_fn();
      })
    } else {
      load_fn()
    }
    function load_fn() {
      //优惠劵请求
      wx.request({
        url: app.url + "api_coupon/couponTopicV1",
        header: {
          'content-type': 'application/json',
          'token': wx.getStorageSync("token_key")
        }, success: function (json) {
          console.log(json.data);
          console.log(json.data.result.adlist);
          that.setData({
            coupon_lists: json.data.result.couponList,
            coupon_banner: json.data.result.adlist,
          });
        }
      })

      //广告模板请求
      wx.request({
        url: app.url + "api_goods/indexTopic",
        header: {
          'content-type': 'application/json',
          'token': wx.getStorageSync("token_key")
        }, success: function (json) {
          console.log(json.data);
          that.setData({
            picture: json.data.result,
          })
        }
      })
    }
  },

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
  onReady: function () {

  },

	/**
	 * 生命周期函数--监听页面显示
	 */
  onShow: function () {

  },

	/**
	 * 生命周期函数--监听页面隐藏
	 */
  onHide: function () {

  },

	/**
	 * 生命周期函数--监听页面卸载
	 */
  onUnload: function () {

  },

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
  onPullDownRefresh: function () {

  },

	/**
	 * 页面上拉触底事件的处理函数
	 */
  onReachBottom: function () {

  },

	/**
	 * 用户点击右上角分享
	 */
  onShareAppMessage: function () {

  }
})